package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import cn.hutool.crypto.SecureUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class YouDaoChannelHelper {


    public static String covertAqyYdUrl(AccountDTO accountDTO, ReportDTO reportDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("ip", reportDTO.getIp());
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        if(StringUtils.isNotBlank(reportDTO.getOs())&&!reportDTO.getOs().contains("__")) {
            params.put("os", reportDTO.getOs());
        }else{
            params.put("os", "android");
        }
        params.put("ts", reportDTO.getTraceTime());
        params.put("model", "");
        params.put("idfa", "");
        params.put("idfa_md5", "");
        params.put("caid_idfa", "");
        params.put("caid", "");
        params.put("aaid", "");
        params.put("android_id", "");
        params.put("android_id_md5", "");
        params.put("developer_id", "9bc124de24e2416c");


        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !reportDTO.getOaidSum().contains("__")) {
            params.put("oaid_md5", reportDTO.getOaidSum());
        }

        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            params.put("oaid", reportDTO.getOaid());
            params.put("oaid_md5", SecureUtil.md5(reportDTO.getOaid()));
        }


        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !reportDTO.getImeiSum().contains("__")) {
            params.put("imei", reportDTO.getImeiSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getIdfaSum()) && !reportDTO.getIdfaSum().contains("__")){
            params.put("idfa_md5", reportDTO.getIdfaSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getIdfa()) && !reportDTO.getIdfa().contains("__")){
            params.put("idfa", reportDTO.getIdfa());
            params.put("idfa_md5", SecureUtil.md5(reportDTO.getIdfa()));
        }


        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) && !reportDTO.getHash_android_id().contains("__")){
            params.put("android_id_md5", reportDTO.getHash_android_id());
        }

        if(StringUtils.isNotBlank(reportDTO.getCaidSum()) && !reportDTO.getCaidSum().contains("__")){
            params.put("caid_md5", reportDTO.getCaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getCaid()) && !reportDTO.getCaid().contains("__")){
            params.put("caid", reportDTO.getCaid());
            params.put("caid_md5", SecureUtil.md5( reportDTO.getCaid()));
        }

        params.put("reqid", "");

        params.put("imei", StringUtils.isNotBlank(reportDTO.getImei()) ? reportDTO.getImei() : "");
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = URLUtil.buildQuery(params, StandardCharsets.UTF_8) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;

    }
}
